package city.spring.modules.oldman.service.impl;

import city.spring.modules.oldman.entity.OldmanAttendanceEntity;
import city.spring.modules.oldman.repository.OldmanAttendanceRepository;
import city.spring.modules.oldman.service.OldmanAttendanceService;
import city.spring.modules.oldman.vo.OldmanAttendanceVO;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Service：老人护理级别
 *
 * @author 谢金婷
 * @date 2020-08-18 14:44:39
 */
@CacheConfig(cacheNames = {OldmanAttendanceServiceImpl.CACHE_NAME})
@Transactional(rollbackFor = Exception.class)
@Service
public class OldmanAttendanceServiceImpl extends ServiceImpl<OldmanAttendanceRepository, OldmanAttendanceEntity> implements OldmanAttendanceService {

    private final OldmanAttendanceRepository oldmanAttendanceRepository;

    public OldmanAttendanceServiceImpl(OldmanAttendanceRepository oldmanAttendanceRepository) {
        this.oldmanAttendanceRepository = oldmanAttendanceRepository;
    }

    @Override
    public void saveOldmanAttendance(OldmanAttendanceEntity entity) {
        if (!save(entity)) {
            throw new RuntimeException("保存信息失败");
        }
    }

    @Override
    public void updateOldmanAttendance(OldmanAttendanceEntity entity) {
        // 使用 updateById(entity); 修改数据时，将会修改 entity 对象中所有非null数据，如果某个字段为null，将会忽略该字段的修改
        if (!updateById(entity)) {
            throw new RuntimeException("修改信息失败");
        }
    }

    @Override
    public void deleteOldmanAttendance(String primaryKey) {
        removeById(primaryKey);
    }

    @Override
    public void deleteOldmanAttendance(List<String> primaryKeys) {
        removeByIds(primaryKeys);
    }

    @Override
    public OldmanAttendanceEntity getAttendanceInfoByoldmanId(String oldmanId) {
        return oldmanAttendanceRepository.getAttendanceInfoByoldmanId(oldmanId);
    }

    @Override
    public OldmanAttendanceVO getAttendanceByPersonId(String personId) {
        return baseMapper.getAttendanceByPersonId(personId);
    }
}